package orchestration.android.parcelable;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;


import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * @author  <a href="mailto:car.ferrarini@gmail.com">Carlo Ferrarini</a>
 */
public class ParcelableAndroidTaskDTO implements Parcelable{
	//TODO add taskType!
	/**
	 * @uml.property  name="taskId"
	 */
	private String taskId;
	/**
	 * @uml.property  name="taskName"
	 */
	private String taskName;
	/**
	 * @uml.property  name="annotations"
	 */
	private HashMap<String,ParcelableAnnotation> annotations;
	/**
	 * @uml.property  name="inputProperties"
	 */
	private HashMap<String,ParcelableProperty> inputProperties;
	/**
	 * @uml.property  name="outputProperties"
	 */
	private HashMap<String,ParcelableProperty> outputProperties;

	/**
	 * @return
	 * @uml.property  name="taskId"
	 */
	public String getTaskId() {
		return taskId;
	}


	/**
	 * @param taskId
	 * @uml.property  name="taskId"
	 */
	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}


	/**
	 * @return
	 * @uml.property  name="taskName"
	 */
	public String getTaskName() {
		return taskName;
	}


	/**
	 * @param taskName
	 * @uml.property  name="taskName"
	 */
	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}


	/**
	 * @return
	 * @uml.property  name="annotations"
	 */
	public HashMap<String, ParcelableAnnotation> getAnnotations() {
		return annotations;
	}


	/**
	 * @param annotations
	 * @uml.property  name="annotations"
	 */
	public void setAnnotations(HashMap<String, ParcelableAnnotation> annotations) {
		this.annotations = annotations;
	}


	/**
	 * @return
	 * @uml.property  name="inputProperties"
	 */
	public HashMap<String, ParcelableProperty> getInputProperties() {
		return inputProperties;
	}


	/**
	 * @param inputProperties
	 * @uml.property  name="inputProperties"
	 */
	public void setInputProperties(
			HashMap<String, ParcelableProperty> inputProperties) {
		this.inputProperties = inputProperties;
	}


	/**
	 * @return
	 * @uml.property  name="outputProperties"
	 */
	public HashMap<String, ParcelableProperty> getOutputProperties() {
		return outputProperties;
	}


	/**
	 * @param outputProperties
	 * @uml.property  name="outputProperties"
	 */
	public void setOutputProperties(
			HashMap<String, ParcelableProperty> outputProperties) {
		this.outputProperties = outputProperties;
	}


	@Override
	public int describeContents() {
		return 0;
	}
	
	public static final Parcelable.Creator<ParcelableAndroidTaskDTO> CREATOR
    = new Parcelable.Creator<ParcelableAndroidTaskDTO>() {
		public ParcelableAndroidTaskDTO createFromParcel(Parcel in) {
		    ParcelableAndroidTaskDTO dto= new ParcelableAndroidTaskDTO();
			dto.setTaskId(in.readString());
			dto.setTaskName(in.readString());
			HashMap<String, ParcelableAnnotation> ann = new HashMap<String, ParcelableAnnotation>();
			String[] annKeys= in.createStringArray();
		    Bundle annB= in.readBundle();
			    for(String key : annKeys)
			    	ann.put(key, (ParcelableAnnotation)annB.getParcelable(key));
			dto.setAnnotations(ann);
			HashMap<String, ParcelableProperty> input = new HashMap<String, ParcelableProperty>();
			String[] inputKeys= in.createStringArray();
		    Bundle inputB= in.readBundle();
			    for(String key : inputKeys)
			    	input.put(key, (ParcelableProperty)inputB.getParcelable(key));
			dto.setInputProperties(input);
			HashMap<String, ParcelableProperty> output = new HashMap<String, ParcelableProperty>();
			String[] outputKeys= in.createStringArray();
		    Bundle outputB= in.readBundle();
			    for(String key : outputKeys)
			    	output.put(key, (ParcelableProperty)outputB.getParcelable(key));
			dto.setInputProperties(input);
			return dto;
		}
		
		public ParcelableAndroidTaskDTO[] newArray(int size) {
		    return new ParcelableAndroidTaskDTO[size];
		}
	};
	
	
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(getTaskId());
		dest.writeString(getTaskName());
		writeMapAsBundle(annotations, dest);
		writeMapAsBundle(inputProperties, dest);
		writeMapAsBundle(outputProperties, dest);
	}
	
	private static void writeMapAsBundle(Map<String,? extends Parcelable> map,Parcel dest ){
		Set<String> keySet = map.keySet();
		Bundle b= new Bundle();
		for(String key : keySet)
			b.putParcelable(key, map.get(key));
		dest.writeStringArray((String[])keySet.toArray());
		dest.writeBundle(b);
	}

}
